# -*- coding: utf-8 -*-
# @Author  : ydf
# @Time    : 2019/10/8 0008 18:32
"""
类装饰器
"""
import types
from functools import wraps
from monkey_print2 import print



class Profiled:
    def __init__(self, func):
        wraps(func)(self)
        self.ncalls = 0

    def __call__(self, *args, **kwargs):
        self.ncalls += 1
        return self.__wrapped__(*args, **kwargs)

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            return types.MethodType(self, instance)

@Profiled
def add(x, y):
    return x + y

class Spam:
    @Profiled
    def bar(self, x):
        print(self, x)

if __name__ == '__main__':
    add(2, 3)
    add(4, 5)
    print(add.ncalls)

    s = Spam()
    s.bar(1)
    s.bar(2)
    s.bar(3)
    print(Spam.bar.ncalls)

